package com.itheima.action;


import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;

import com.itheima.domain.Book;
import com.itheima.domain.PageBean;
import com.itheima.service.BookService;
import com.itheima.utils.UUIDUtils;
import com.opensymphony.xwork2.Action;

public class bookAction implements Action{
	
	/**
	 * 分页查询
	 * @return
	 */
	public String findByPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			String cPage = request.getParameter("currPage");
			if(cPage == null || cPage.equals("0")){
				cPage = "1";
			}
			Integer currPage = Integer.parseInt(cPage);
			String bookName = request.getParameter("bookName");
			String s_price = request.getParameter("s_price");
			String e_price = request.getParameter("e_price");
			if(bookName != null && !bookName.equals("") && request.getMethod().toUpperCase().equals("GET")){
					bookName = new String(bookName.getBytes("ISO-8859-1"),"UTF-8");
			}
			//调用Service层处理数据
			BookService bookService = new BookService();
			PageBean<Book> pageBean =  bookService.findBookByPage(currPage,bookName,s_price,e_price);
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("bookName", bookName);
			request.setAttribute("s_price", s_price);
			request.setAttribute("e_price", e_price);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	public String delete(){
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer bid = Integer.parseInt(request.getParameter("bid"));
			BookService bookService = new BookService();
			int a = bookService.deleteBook(bid);
			if(a>0){
				request.setAttribute("msg", "删除成功");
				return "success";
			}else{
				request.setAttribute("msg", "删除失败");
				return "fail";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "fail";
	}
	
	/**
	 * 跳转到添加页面，设置一个令牌，防止重复提交
	 * @return
	 */
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		request.getSession().setAttribute("token", UUIDUtils.getUUID());
		return "success";
	}
	
	public String insert(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String token1 = request.getParameter("token");
		String token2 = (String) request.getSession().getAttribute("token");
		request.getSession().removeAttribute("token");
		if(!token1.equals(token2)){
			request.setAttribute("tip", "不能重复提交数据！！！");
			return "err";
		}
		Map<String,String[]> map = request.getParameterMap();
		Book book = new Book();
		try {
			BeanUtils.populate(book, map);
			BookService bookService = new BookService();
			int a = bookService.addBook(book);
			if(a>0){
				return "success";
			}else{
				return "fail";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String execute() throws Exception {
		return null;
	}

}
